package com.comtime.mybox;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.comtime.service.MyService;
import com.comtime.smartbox.BaseActivity;
import com.comtime.smartbox.HBApplication;
import com.comtime.smartbox.MainActivity;
import com.comtime.smartkilimanjaro.R;
import com.comtime.utils.MySharedPreferences;
import com.comtime.utils.Util;
import com.comtime.view.MyDialog;

public class AboutActivity extends BaseActivity {
	TextView tv_version,tv_device_version;
	MySharedPreferences mySharedPreferences;
	
	Button btn_update_device;
	
	HBApplication application;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_about);
		application=(HBApplication) getApplication();
		mySharedPreferences=MySharedPreferences.getInstance(this);
		init();
		IntentFilter intentFilter=	new IntentFilter(MyService.ACTION_UpdateOADState);
		intentFilter.addAction(MyService.ACTION_UpdateDeviceName);
		registerReceiver(new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				
				
				tv_device_version.setText(application.swDevicesList.get(0).versionName);
				
				
				if(application.swDevicesList.get(0).needOAD){
					btn_update_device.setVisibility(View.VISIBLE);
				}else{
					btn_update_device.setVisibility(View.GONE);
				}
			}
		},intentFilter );
	}

	private void init() {
		//版本号
		tv_version=(TextView) findViewById(R.id.tv_version);
		tv_version.setText(getResources().getString(R.string.version)+Util.getAppVersionName(this));
		
		tv_device_version=(TextView) findViewById(R.id.tv_device_version);
		
		
		tv_device_version.setText(application.swDevicesList.get(0).versionName);
		
		btn_update_device=(Button) findViewById(R.id.btn_update_device);
		btn_update_device.setVisibility(View.GONE);
		
		if(application.swDevicesList.get(0).needOAD){
			btn_update_device.setVisibility(View.VISIBLE);
		}
		
		
	}
	
	public void backAction(View v){
		this.finish();
		overridePendingTransition(R.anim.enter_left, R.anim.exit_right);
	}
	/**
	 * 用户协议
	 * @param v
	 */
	public void UserProtocolAction(View v){
		
	}
	
	
	public void updateDeviceAction(View v){
		 showNeedOADDialog();
	}
	protected void showNeedOADDialog() {
		final MyDialog myDialog;
		myDialog = new MyDialog(this);
		myDialog.setContent(R.string.need_OAD);
		myDialog.setLeftButtonText(R.string.cancel);
		myDialog.setRightButtonText(R.string.ok);
		myDialog.setCancelable(false);
		myDialog.getLeftButton().setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				myDialog.dismiss();
			}
		});
		myDialog.getRighrButton().setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				myDialog.dismiss();
				Intent intent = new Intent(AboutActivity.this, OADActivity.class);
				startActivity(intent);
			}
		});
		myDialog.show();
	}

	
}

